package jpb.module.rest.organize;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.constant.StatusCode;
import jpb.module.daomain.member.Member;
import jpb.module.daomain.organize.Organize;
import jpb.module.service.organize.OrganizeService;
import jpb.security.HandleCookie;
import jpb.security.HandleToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by haol on 2017/4/1.
 */
@RestController
@RequestMapping("/api/web/organize")
public class OrganizeController {

        @Autowired
        private OrganizeService organizeService;

        private HandleToken handleToken = new HandleToken();

        /**
         * @api {POST} /api/web/organize/add 新增组织
         * @apiGroup Organize
         * @apiName add
         * @apiDescription 新增一个组织
         * @apiParam {String} name 组织名(not null)
         * @apiParam {String} mark 组织标识(unique,not null)
         * @apiParam {String} type 组织类型( not null)0为私有，1为私有可分享，2为公共
         * @apiParamExample {json} 请求样例：
         * POST /api/web/organize/add
         * {
         * "name":"软件一班",
         * "mark":"softwareOne",
         * "type":"0"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"新增成功"
         * }
         */
        @RequestMapping(value = "/add", method = RequestMethod.POST)
        public String add(Organize organize, HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String mid = handleToken.getValueByKey(token, "id");

                if (organizeService.getByMark(organize.getMark()) == null) {
                        organizeService.add(organize, mid);
                        return new Reply(0, ReturnState.INSERT_SUCCESS).toJson();
                } else {
                        return new Reply(1, ReturnState.NO_UNIQUE).toJson();
                }


        }

        /**
         * @api {DELETE} /api/web/organize/i/{id}/delete 删除组织
         * @apiGroup Organize
         * @apiName delete
         * @apiDescription 创始人删除组织id为{id}的组织
         * @apiParamExample {json} 请求样例：
         * DELETE /api/web/organize/i/organize_id/delete
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"删除成功"
         * }
         */
        @RequestMapping(value = "/i/{id}/delete", method = RequestMethod.DELETE)
        public String delete(@PathVariable String id) {
                organizeService.delete(id);

                return new Reply(0, ReturnState.DELETE_SUCCESS).toJson();
        }

        /**
         * @api {PUT} /api/web/organize/i/{id} 更新信息
         * @apiGroup Organize
         * @apiName update
         * @apiDescription 更新组织id为{id}的信息
         * @apiParam {String} name 组织名(not null)
         * @apiParam {String} introduce 组织介绍
         * @apiParam {String} tenureTime 组织任期
         * @apiParam {String} officeTel 组织办公电话(not null)
         * @apiParam {String} officeSite 组织办公地址(not null)
         * @apiParam {String} type 组织类型(默认为0私有，1为公开)
         * @apiParamExample {json} 请求样例：
         * PUT /api/web/organize/i/organize_id
         * {
         * "id":"1",
         * "name":"华工广州学院";
         * "share":"1"
         * "introduce":"学校还可以",
         * "officeTel":"8110",
         * "officeSite":"广东省广州市"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"更新成功"
         * }
         */
        @RequestMapping(value = "/i/{id}", method = RequestMethod.PUT)
        public String update(Organize organize) {
                organizeService.complete(organize);
                return new Reply(0, ReturnState.UPDATE_SUCCESS).toJson();
        }

        /**
         * @api {GET} /api/web/organize/i/{id}  获取组织信息
         * @apiGroup Organize
         * @apiName get
         * @apiDescription 获取组织id为{id}的单个组织信息
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":{"id":"c18d6dbc-3158-418e-829c-729c6c32ce9d","mark":"gcu","name":"@","tenureTime":"2006","createTime":"Tue Apr 18 22:05:44 CST 2017","updateTime":"Tue Apr 18 22:06:04 CST 2017","officeSite":"学校地址：广州市花都区学府路一号","officeTel":"招生咨询：020-66609166"}
         * }
         */
        @RequestMapping(value = "/i/{id}", method = RequestMethod.GET)
        public String get(@PathVariable String id) {
                Organize organize = organizeService.get(id);
                Reply<Organize> reply = new Reply<Organize>(0, ReturnState.SUCCESS, organize);
                return reply.toJson();

        }

        /**
         * @api {GET} /api/web/organize/check-mark  检测组织mark
         * @apiGroup Organize
         * @apiName check
         * @apiDescription 检测组织mark是否唯一
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/check-mark
         * @apiSuccessExample {json} 返回样例:
         * {
         *           "true"
         * }
         */
        @RequestMapping(value = "/check-mark", method = RequestMethod.GET)
        public boolean check(String mark) {
                if (organizeService.getByMark(mark) == null) {
                        return true;
                } else {
                        return false;
                }
        }


        /**
         * @api {GET} /api/web/organize/see  所管理的组织
         * @apiGroup Organize
         * @apiName see
         * @apiDescription  组织管理员查看自己所管理的组织
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/see
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":[{"id":"c18d6dbc-3158-418e-829c-729c6c32ce9d","mark":"gcu","name":"华南理工大学广州学院","introduce":"@","tenureTime":"2006","createTime":"Tue Apr 18 22:05:44 CST 2017","updateTime":"Tue Apr 18 22:06:04 CST 2017","officeSite":"学校地址：广州市花都区学府路一号","officeTel":"招生咨询：020-66609166"}]
         * }
         */
        @RequestMapping(value = "/see", method = RequestMethod.GET)
        public String see(HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String mid = handleToken.getValueByKey(token, "id");
                List<Organize> organizes = organizeService.getByMid(mid);

                Reply<List<Organize>> reply = new Reply<List<Organize>>(0, ReturnState.SUCCESS, organizes);
                return reply.toJson();
        }

        /**
         * @api {POST} /api/web/organize/i/{id}/clear  清空信息
         * @apiGroup Organize
         * @apiName clear
         * @apiDescription 创始人清空组织id为{id}的部分信息,公共组织默认清空l全部成员
         * @apiParam {int} choice 选择清空的类型，1为成员，2为部门
         * @apiParamExample {json} 请求样例：
         * POST /api/web/organize/i/organize_id/clear?choice=1
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"清空成功"
         * }
         */
        @RequestMapping(value = "/i/{id}/clear", method = RequestMethod.POST)
        public String clear(Integer choice, @PathVariable String id) {
                Organize organize = organizeService.get(id);
                // 清空成员，清空部门
                if (choice == null) {
                        return new Reply(1, "未选择").toJson();
                }
                //清空成员关系
                if (choice == 1) {
                        organizeService.deleteOrgMem(id);
                        if (organize.getType() == 2){
                                organizeService.deleteMem(id);
                        }
                        return new Reply(0, "清空成员成功").toJson();
                        //删除部门及其关系
                } else if (choice == 2) {
                        organizeService.deleteDep(id);
                        return new Reply(0, "清空部门成功").toJson();
                }
                return new Reply(1, ReturnState.FAIL).toJson();
        }


        /**
         * @api {POST} /api/web/department/i/{oid}/del-mem 删除成员ByNum
         * @apiGroup Organize
         * @apiName delMem
         * @apiDescription 删除组织id为{oid}的组织中的编号为memberNum的成员
         * @apiParam {String} memberNum 成员编号(not nul)
         * @apiParamExample {json} 请求样例：
         * POST /api/web/organize/i/organize_id/del-mem
         * {
         * "memberNum":""201430098009";
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"删除成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}/del-mem", method = RequestMethod.POST)
        public String delMem(@PathVariable String oid, String memberNum) {
                organizeService.delMem(oid, memberNum);
                return new Reply(0, ReturnState.DELETE_SUCCESS).toJson();
        }
        /**
         * @api {POST} /api/web/organize/i/{oid}/{id}/mem 增加成员
         * @apiGroup Department
         * @apiName addMem
         * @apiDescription 为id为{oid}的组织中部门id为{id}的部门增加成员，如果为pub类型的组织则 proName可以为空
         * @apiParam {String} memberNum 成员编号(not nul)
         * @apiParam {String} proName 成员职称(非公共组织not nul)
         * @apiParam {String} oid 组织id
         * @apiParam {Integer} did 部门id
         * @apiParamExample {json} 请求样例：
         * POST /api/web/organize/i/organize_id/parent_id/mem
         * {
         * "memberNum":"201430098009";
         * "proName":"国家主席",
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"增加成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}/{did}/mem", method = RequestMethod.POST)
        public String addMem(@PathVariable String oid, @PathVariable Integer did,
                             String memberNum, String proName) {
                Reply reply = null;
                Integer pid = 0;
                if (StringUtils.isNotBlank(proName)) {
                        pid = organizeService.getPidByName(oid, proName);
                        if (pid == null) {
                                return new Reply(1, "该职称名找不到,请先增加职称").toJson();
                        }
                }
                if (organizeService.checkDepIsDeaf(did)) {
                        organizeService.addMem(oid, did, memberNum, pid);
                        reply = new Reply(0, ReturnState.SUCCESS);
                } else {
                        reply = new Reply(1, ReturnState.INSERT_FAIL + "成员只能增加到叶子部门");
                }
                return reply.toJson();
        }



        /**
         * @api {PUT} /api/web/member/i/{oid}/pro 更新成员职称
         * @apiGroup MemberWeb
         * @apiName updatePro
         * @apiDescription 更新成员个人职称信息
         * @apiParam {String} proName 职称名(not null)
         * @apiParam {Integer} proId 职称Id(职称名和职称id只能有一个为null)
         * @apiParam {String} memberNum 成员编号(not null)
         * @apiParam {String} oid 组织Id (not null)
         * @apiParamExample {json} 请求样例：
         * PUT /api/web/member/i/organize_id/pro
         * {
         *      “id":"1",
         *      "memberNum":"201430098009",
         *      "proName";"部长"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"更新成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}/pro", method = RequestMethod.PUT)
        public String updatePro(@PathVariable String oid, String proName,
                                String memberNum,Integer pid) {
                Reply reply = null;
                if (pid == null){
                        pid = organizeService.getPidByNameAndOid(oid, proName);
                }
                if (pid == null) {
                        reply = new Reply(1, ReturnState.NO_FIND);
                } else {
                        organizeService.updatePro(memberNum, oid, pid);
                        reply = new Reply(0, ReturnState.UPDATE_SUCCESS);
                }

                return reply.toJson();
        }


        /**
         * @api {GET} /api/web/organize/i/{id}/mem 获取成员信息ByOid
         * @apiGroup Organize
         * @apiName getMemByOid
         * @apiDescription 获取组织id为{id}组织中的全部学生信息
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id/mem
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":[{"memberNum":"201430098002"},{"id":1,"name":"张睿浩","memberNum":"201430098009","createTime":"Thu Apr 20 16:55:35 CST 2017","updateTime":"Thu Apr 20 20:37:00 CST 2017","mobiles":["123456","123231221"]},{"memberNum":"201430098017"}]
         * }
         */
        @RequestMapping(value = "/i/{id}/mem", method = RequestMethod.GET)
        public String getMemByOid(@PathVariable String id) {
                List<Member> members = null;
                members = organizeService.getMem(id);
                if (members.size() == 0){
                        return new Reply<List<Member>>(3,ReturnState.NO_DATA ,members).toJson();
                }
                return new Reply<List<Member>>(0, ReturnState.SUCCESS, members).toJson();

        }

        /**
         * @api {GET} /api/web/organize/i/{id}/mem 获取成员信息ByDid
         * @apiGroup Organize
         * @apiName getMemByDid
         * @apiDescription 获取部门id为{id}组织中的全部学生信息(只搜索该id下的，不搜索其儿子)
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id/mem?did =9
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":[{"memberNum":"201430098002"},{"id":1,"name":"张睿浩","memberNum":"201430098009","createTime":"Thu Apr 20 16:55:35 CST 2017","updateTime":"Thu Apr 20 20:37:00 CST 2017","mobiles":["123456","123231221"]},{"memberNum":"201430098017"}]
         * }
         */
        @RequestMapping(value = "/i/{id}/{did}/mem", method = RequestMethod.GET)
        public String getMemByDid(@PathVariable Integer did) {
                List<Member> members = null;
                members = organizeService.getByDid(did);
                if (members.size() == 0){
                        return new Reply<List<Member>>(3,ReturnState.NO_DATA ,members).toJson();
                }
                return new Reply<List<Member>>(0, ReturnState.SUCCESS, members).toJson();

        }
        /**
         * @api {GET} /api/web/organize/i/{id}/follow 获取关注的成员
         * @apiGroup Organize
         * @apiName getMemByOid
         * @apiDescription 获取关注了组织id为{id}的组织
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id/follow
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":[{"memberNum":"201430098002"},{"id":1,"name":"张睿浩","memberNum":"201430098009","createTime":"Thu Apr 20 16:55:35 CST 2017","updateTime":"Thu Apr 20 20:37:00 CST 2017","mobiles":["123456","123231221"]},{"memberNum":"201430098017"}]
         * }
         */
        @RequestMapping(value = "/i/{id}/follow", method = RequestMethod.GET)
        public String getFollowMem(@PathVariable String id) {
                List<Member> members = null;
                members = organizeService.getFollowMem(id);
                if (members.size() == 0){
                        return new Reply<List<Member>>(3,ReturnState.NO_DATA ,members).toJson();
                }
                return new Reply<List<Member>>(0, ReturnState.SUCCESS, members).toJson();

        }
        @RequestMapping(value = "/i/{id}/update-logo")
        public String updateLogo(Organize organize) {
                organizeService.updateLogo(organize);
                return ReturnState.UPDATE_SUCCESS;
        }
}
